<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select class="filter-item" style="width: 200px" v-model="listQuery.proxyBrandId" placeholder="品牌" clearable>
        <el-option
          v-for="children in brandOption" :key="children.value" :label="children.label" :value="children.value">
        </el-option>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleAdd">
        添加
      </el-button>
    </div>
      <el-table :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
        <el-table-column label="公司名称" prop="clientCompanyName" align="center" />
        <el-table-column label="手机号" prop="clientMobile" align="center" />
        <el-table-column label="联系人" prop="clientName" align="center" />
        <el-table-column label="品牌" prop="proxyBrandText" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
    
    <el-dialog :title="title" :visible.sync="dialogFormVisible" append-to-body>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="88px"
        style="margin: 0 50px;">
        <el-form-item label="公司名称" prop="clientCompanyName">
          <el-input v-model="temp.clientCompanyName" />
        </el-form-item>
        <el-form-item label="手机号" prop="clientMobile">
          <el-input v-model="temp.clientMobile" />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input v-model="temp.clientName" />
        </el-form-item>
        <el-form-item label="品牌" prop="proxyBrandId">
          <el-select style="width: 200px" v-model="temp.proxyBrandId" placeholder="品牌" clearable>
            <el-option
              v-for="children in brandOption" :key="children.value" :label="children.label" :value="children.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getZcyxAgentDetailList, saveAgentDetail, updateAgentDetail, deleteAgentDetail, getProxyBrandList } from '@/api/product'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'zcyxAgentList',
  directives: { waves },
  components: { Pagination },
  filters: {
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        proxyBrandId: '', 
        pageNo: 1,
        pageSize: 20,
        gongyingId: '', 
        type: 1, 
        clientType: 2
      },
      temp: {
        clientCompanyName: '',
        clientMobile: '',
        clientName: '',
        proxyBrandId:'',
        clientType: 2
      },
      rules: {
        clientCompanyName: [{ required: true, message: '请输入', trigger: 'blur' }],
        clientMobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        clientName: [{ required: true, message: '请输入', trigger: 'blur' }],
        proxyBrandId: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      dialogFormVisible: false,
      title: '',
      brandOption: [],
      brandQuery: {
        pageNo: 1,
        pageSize: 999,
      },
    }
  },
  watch: {
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.getBrandData()
    this.handleFilter()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      this.listQuery.gongyingId = this.userInfo.id 
      
      getZcyxAgentDetailList(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    getBrandData() {
      getProxyBrandList(this.brandQuery).then((response) => {
        this.brandOption = response.list.map((item) => {
          return { value: item.id, label: item.name }
        })
      });
    },
    handleAdd() {
      this.temp.clientCompanyName = ''
      this.temp.clientMobile = ''
      this.temp.clientName = ''
      this.dialogFormVisible = true
      this.title = "添加"
      this.getBrandData()
    },
    handleEdit(row) {
      this.temp = Object.assign({}, row)
      this.dialogFormVisible = true
      this.title = "编辑"
      this.getBrandData()
    },
    handleDelete(row) {
      deleteAgentDetail({ids: row.id}).then(response => { 
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success'
        });
      }).catch(error => {
        console.log(error);
      })
    },
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.title === '添加') {
            delete this.temp.createTime
            this.temp.clientType = 2
            saveAgentDetail(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false

              this.$message({
                message: '保存成功',
                type: 'success'
              });
            })
          } else {
            delete this.temp.createTime
            this.temp.clientType = 2
            updateAgentDetail(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false

              this.$message({
                message: '保存成功',
                type: 'success'
              });
            })
          }
        }
      })
    },
  }
}
</script>
